﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using HandlingInput;
using GameCore;

namespace GameObject
{
    public class Button : Window
    {

        int count = 0;//thoi gian cho
        public override event Window.GetEvent Enter;
        public override event Window.GetEvent Hover;
        public override event Window.GetEvent Normal;


        private bool enter_flag = true; //co nhan su kien enter(click

        protected bool show_text = true; //
        /// <summary>
        /// Hàm khởi tạo button
        /// </summary>
        /// <param name="_numberSprite">Số lượng Sprite</param>
        /// <param name="strResource">Danh sách các ảnh sẽ được load vào 1 sprite</param>
        /// <param name="textDisplay">Tên của ảnh sẽ được hiển thị</param>
        /// <param name="font">Font của text - koo dùng</param>
        /// <returns></returns>
        public override bool Init(int _numberSprite, string[] strResource, string textDisplay, SpriteFont font)
        {
            TextDisplay = textDisplay;
            Font = font;
            _nsprite = _numberSprite;
            _sprite = new Sprite[_nsprite+1];
            _lsprite = new List<Vector2>();

            for (int i = 0; i < _nsprite; i++)
            {
                Texture2D[] texture = new Texture2D[strResource.Length];
                for (int j = 0; j < strResource.Length; j++)
                {
                    texture[j] = GlobalDefine.Content.Load<Texture2D>(@"Pictures/" + strResource[j]);
                }
                _sprite[i] = new Sprite(0, 0, 0, 0, texture);
            }

            Texture2D[] tt2d = new Texture2D[1];
            tt2d[0] = GlobalDefine.Content.Load<Texture2D>(@"Pictures/" + TextDisplay);
            _sprite[1] = new Sprite(0, 0, 0, 0, tt2d);
            _nsprite += 1;
            return true;

        }
        public override void  Update(GameTime gameTime, float x, float y, int w, int h)
        {
            this.X = x;
            this.Y = y;
            this.Width = w;
            this.Height = h;

            EventHandle.Update();

            if (Enable)
            {
                // Kiểm tra xem có event hay không
                bool a = false;
                if (this._lsprite.Count > 0)
                    if ((int)(_lsprite[0].Y) == s_click)
                        if (Enter != null)
                            a = true;

                if (a && enter_flag)
                {
                    count++;
                    if (count % 10 == 0)
                    {
                        GetEvent handle = Enter;

                        if (handle != null)
                            handle(this, GlobalDefine.now_gamestate);
                        enter_flag = false;
                    }
                }
                else
                {
                    enter_flag = true;
                    this._lsprite.Clear();


                    if (EventHandle.MouseLeftClick(new Rectangle((int)this.X - ViewFrame.window.X, (int)this.Y - ViewFrame.window.Y, (int)this.Width, (int)this.Height)))
                    {
                        _lsprite.Add(new Vector2(0, s_click));

                    }
                    else
                        if (EventHandle.MouseOver(new Rectangle((int)this.X - ViewFrame.window.X, (int)this.Y - ViewFrame.window.Y, (int)this.Width, (int)this.Height)))
                        {
                            _lsprite.Add(new Vector2(0, s_hover));

                            GetEvent handle = Hover;

                            if (handle != null)
                                handle(this, GlobalDefine.now_gamestate);
                        }
                        else
                        {
                            _lsprite.Add(new Vector2(0, s_normal));

                            GetEvent handle = Normal;

                            if (handle != null)
                                handle(this, GlobalDefine.now_gamestate);
                        }
                    if (show_text)
                        _lsprite.Add(new Vector2(1, 0));
                }
            }
            base.Update(gameTime, x, y, w, h);

         
        }

        public override VisibleGameObject Clone()
        {
            VisibleGameObject newObj = new Button();
            newObj._nsprite = this._nsprite;
            ((Button)newObj).TextDisplay = this.TextDisplay;
            ((Button)newObj).Font = this.Font;
            newObj._sprite = new Sprite[_nsprite];
            for (int i = 0; i < _nsprite; i++)
            {
                newObj._sprite[i] = _sprite[i].Clone() ;
            }
            newObj.X = this.X;
            newObj.Y = this.Y;
            newObj.Width = this.Width;
            newObj.Height = this.Height;
            newObj._lsprite = new List<Vector2>();
            return newObj;
        }
    }
}
